using System.Collections;
using Mistral.ComponentModel;
using Mistral.Interface;
using Mistral.Interface.Features;
using Mistral.Options;

namespace Mistral.Plugin
{
	class CategoryView : IGetPropertyGridView, ITreeNodeDescriptor
	{
		readonly IOptionCategory _category;

		public CategoryView( IOptionCategory option )
		{
			_category = option;
		}
			
		#region IGetPropertyGridView Members

		public object GetPropertyGridView()
		{
			VirtualObject view = new VirtualObject();

			string category = OptionsService.GetFullName( _category, "." );
			foreach ( IOption option in _category.Options )
			{
				CustomProperty property = new CustomProperty();
				property.Name = option.Name;
		
				if( option is ICollectionOption )
				{
					property.PropertyType = option.Value.GetType();
					property.ValueProvider = new ValueProvider_PropertyInfo( option, "Values" );
				}
				else
				{
					property.PropertyType = option.GetValueType();
					property.ValueProvider = new ValueProvider_PropertyInfo( option, "Value" );
				}
				IGetPropertyGridView optionGetView = (IGetPropertyGridView)App.Instance.ServiceManager.GetService( option, typeof ( IGetPropertyGridView ) );
				if( optionGetView != null )
				{
					VirtualObject optionView = (VirtualObject)optionGetView.GetPropertyGridView();
					foreach ( CustomProperty customProperty in optionView.Properties )
					{
						customProperty.Category = category;
						view.AddProperty( customProperty );
					}
					
					continue;
					//property.PropertyType = optionView.GetType();
					//property.ValueProvider = new ValueProvider_Simple( optionView );
				}


				property.Category = category;
				property.Description = option.Description;

				view.AddProperty( property );
				
			}

			return view;
		}

		#endregion

		#region ITreeNodeDescriptor Members

		public string Name
		{
			get { return _category.Name; }
		}

		public object Tag
		{
			get { return _category; }
		}

		public IEnumerable Children
		{
			get
			{
				ArrayList list = new ArrayList();
				foreach ( IOptionCategory category in _category.Categories )
				{
					list.Add( category );
				}
				foreach ( IOption option in _category.Options )
				{
					list.Add( option );
				}				
				return list;
			}
		}

		#endregion
	}
}